﻿//////////////////////////////////////////////////////////////////////////////
//
// Copyright © 1998-2024 Glodon Company Limited.
//
// Licensed under the MIT License
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the “Software”),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
//////////////////////////////////////////////////////////////////////////////
#include "TestJournalPropertyPaletteReplayer.h"
#include "IJournalUiObject.h"
#include "DebugMessage.h"
#include "StringUtil.h"
#include "GmJournalConfig.h"
#include "QApplication"
#include "GbmpNew.h"
#include <QMouseEvent>

#include "EnableCompileWarning_The_LAST_IncludeInCpp.h"
using namespace gcmp;

gcmp::OwnerPtr<QMouseEvent> CreateMouseEvent(const std::wstring &cmdName, const QString &buttonType, int x, int y, int modifier)
{
    Qt::MouseButton mouseButton = Qt::LeftButton;
    if (buttonType == "Right")
    {
        mouseButton = Qt::RightButton;
    }
    else if (buttonType == "Middle")
    {
        mouseButton = Qt::MiddleButton;
    }
    else if (buttonType == "No")
    {
        mouseButton = Qt::NoButton;
    }
    QPoint pos(x, y);

    if (cmdName == JRN_EVENT_TYPE_MOUSE_PRESS)
    {
        return NEW_AS_OWNER_PTR(QMouseEvent, QEvent::MouseButtonPress, pos, mouseButton, mouseButton, Qt::KeyboardModifier(modifier));
    }
    else if (cmdName == JRN_EVENT_TYPE_MOUSE_RELEASE)
    {
        return NEW_AS_OWNER_PTR(QMouseEvent, QEvent::MouseButtonRelease, pos, mouseButton, mouseButton, Qt::KeyboardModifier(modifier));
    }
    else if (cmdName == JRN_EVENT_TYPE_MOUSE_DOUBLECLICK)
    {
        return NEW_AS_OWNER_PTR(QMouseEvent, QEvent::MouseButtonDblClick, pos, mouseButton, mouseButton, Qt::KeyboardModifier(modifier));
    }
    else if (cmdName == JRN_EVENT_TYPE_MOUSE_LEAVE)
    {
        return NEW_AS_OWNER_PTR(QMouseEvent, QEvent::Leave, pos, mouseButton, mouseButton, Qt::KeyboardModifier(modifier));
    }
    return nullptr;
}

TestJournalPropertyPaletteReplayer::TestJournalPropertyPaletteReplayer()
{
}

TestJournalPropertyPaletteReplayer::~TestJournalPropertyPaletteReplayer(void)
{
}

bool gcmp::TestJournalPropertyPaletteReplayer::Replay(IJournalUiObject * pIUiObject, std::wstring eventType, const std::map<std::wstring, std::wstring> & journalParameter)
{
    DBG_WARN_AND_RETURN_UNLESS(pIUiObject, false, L"输入pIUIObject不应该有为空的情况",L"GDMPLab",L"2024-03-30");

    QObject *pObject = pIUiObject->GetUiObject().cast<QObject*>();
    DBG_WARN_AND_RETURN_UNLESS(pObject, false, L"输入pObject中存储的控件对象类型不是QObject",L"GDMPLab",L"2024-03-30");

    if (eventType == JRN_EVENT_TYPE_MOUSE_PRESS || eventType == JRN_EVENT_TYPE_MOUSE_RELEASE || eventType == JRN_EVENT_TYPE_MOUSE_DOUBLECLICK || eventType == JRN_EVENT_TYPE_MOUSE_LEAVE)
    {
        auto iterBtnType = journalParameter.find(JRN_PARAM_BUTTON_TYPE);
        DBG_WARN_AND_RETURN_UNLESS(iterBtnType != journalParameter.end(), false, L"不存在键为宏JRN_PARAM_BUTTON_TYPE的参数",L"GDMPLab",L"2024-03-30");
        QString buttonType = QString::fromStdWString(iterBtnType->second);

        auto iterX = journalParameter.find(JRN_PARAM_MOUSE_POSE_X);
        DBG_WARN_AND_RETURN_UNLESS(iterX != journalParameter.end(), false, L"不存在键为宏JRN_PARAM_MOUSE_POSE_X的参数",L"GDMPLab",L"2024-03-30");
        int x = StringUtil::ToNumber<int>(iterX->second);

        auto iterY = journalParameter.find(JRN_PARAM_MOUSE_POSE_Y);
        DBG_WARN_AND_RETURN_UNLESS(iterY != journalParameter.end(), false, L"不存在键为宏JRN_PARAM_MOUSE_POSE_Y的参数",L"GDMPLab",L"2024-03-30");
        int y = StringUtil::ToNumber<int>(iterY->second);

        gcmp::OwnerPtr<QMouseEvent> mouseEvent = CreateMouseEvent(eventType, buttonType, x, y, 0);
        if (mouseEvent)
        {
            // 这个操作是为了在回放时区分回放产生事件与用户事件（用户事件默认为true）
            mouseEvent->setAccepted(false);
            QApplication::sendEvent(pObject, mouseEvent.get());
        }
    }

    return true;
}

